<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="base/base :: base_head" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>测试websocket</title>
    <!--    <link rel="stylesheet" type="text/css"
              data-th-href="@{/styles/common.css}" />
    -->
    <script type="text/javascript"
            data-th-src="@{/scripts/MyUtil.js}"></script>
<!--    <script type="text/javascript"
            data-th-src="@{/scripts/main/ScanTestView.js}"></script>-->
    <!--    <script type="text/javascript"
                data-th-src="@{/lang/lang_zh_CN.js}"></script>-->
    <script type="text/javascript">
        /*var mainView;
        EUI.onReady(function () {
            mainView = new EUI.ScanTestView({
                renderTo: "content",
            });
        });*/

        var userId = "111";
        var userName = "Qyb";
        var ws = null;
        var target = ('ws://localhost:9010/echo?userId='+userId);
/*        function setConnected(connected) {
            document.getElementById('connect').disabled = connected;
            document.getElementById('disconnect').disabled = !connected;
            document.getElementById('echo').disabled = !connected;
        }*/

        function connect() {
            if (target == '') {
                alert('Please select server side connection implementation.');
                return;
            }
            if ('WebSocket' in window) {
                ws = new WebSocket(target);
            } else if ('MozWebSocket' in window) {
                ws = new MozWebSocket(target);
            } else {
                alert('WebSocket is not supported by this browser.');
                return;
            }
            ws.onopen = function () {
               /* setConnected(true);*/
                console.log('Info: WebSocket connection opened.');
            };
            ws.onmessage = function (event) {
                console.log('Received: ' + event.data);
            };
            ws.onclose = function (event) {
                /*setConnected(false);*/
                console.log('Info: WebSocket connection closed, Code: ' + event.code + (event.reason == "" ? "" : ", Reason: " + event.reason));
            };

            window.onbeforeunload = function(event) {
                ws.close();
            }
        }
        function send() {
            var msg = document.getElementById("msg").value;
            ws.send(msg);
        }

    </script>
</head>
<body>
<!--<div id="content" style="background-color: white"></div>-->
<button id="connect" onclick="connect();">Connect</button>
<input id="msg" /><button id="send" onclick="send();">send</button>
</body>
</html>